// pages/groupSpace/publisharticle/publisharticle.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picker: ['今日推荐', '车趣杂谈', '车友生活', '香车美女'],
    pickerValue: '请先选择话题',
    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const isIOS = app.globalData.platform === 'ios'
    this.setData({
      isIOS: isIOS
    })
    const that = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange(res => {
      if (res.height === keyboardHeight) return
      const duration = res.height > 0 ? res.duration * 1000 : 0
      keyboardHeight = res.height
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: 0,
          success() {
            that.updatePosition(keyboardHeight)
            that.editorCtx.scrollIntoView()
          }
        })
      }, duration)

    })
  },
  updatePosition(keyboardHeight) {
    const toolbarHeight = 50;
    let editorHeight = keyboardHeight > 0 ? (app.globalData.windowHeight - keyboardHeight - toolbarHeight) : app.globalData.windowHeight
    this.setData({
      editorHeight,
      keyboardHeight
    })
  },
  calNavigationBarAndStatusBar() {
    const navigationBarHeight = this.data.isIOS ? 44 : 48
    return app.globalData.StatusBar + navigationBarHeight
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },
  //插入图片
  insertImage() {
    let self = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        self.editorCtx.insertImage({
          src: res.tempFilePaths[0],
          data: {
            id: 'abcd',
            role: 'god'
          },
          width: '100%',
          success: function () {
            console.log('insert image success');
          }
        })
      }
    })
  },
  blur() {
    this.editorCtx.blur()
  },
  format(e) {
    let {
      name,
      value
    } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value);
  },
  onEditorInput(e) {
    let self = this;
    console.log(e);
  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({
      formats
    });
    console.log('formats', formats);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  pickerChange(e) {
    let self = this;
    console.log(e);
    let picker = self.data.picker;
    this.setData({
      pickerValue: picker[e.detail.value]
    })
  },
})